class Solution {
    public int[] dailyTemperatures(int[] temperatures) {

        int len = temperatures.length;
        int[] nextHigherTempIndex = new int[len];
        int[] res = new int[len];
        
        nextHigherTempIndex[len - 1] = Integer.MAX_VALUE;
        res[len - 1] = 0;
        for(int i = len - 2; i >= 0; i--){
            if(temperatures[i] < temperatures[i + 1]){
                
                nextHigherTempIndex[i] = i + 1;
                res[i] = 1;
            }else{
                
                int curIndex = i + 1;
                while(curIndex < len && temperatures[i] >= temperatures[curIndex]){
                    curIndex = nextHigherTempIndex[curIndex];
                }
                
                if(curIndex == Integer.MAX_VALUE){
                   
                    nextHigherTempIndex[i] = Integer.MAX_VALUE;
                    res[i] = 0;
                }else{
                    
                    nextHigherTempIndex[i] = curIndex;
                    res[i] = curIndex - i;
                }
            }
        }
        return res;
    }
}

